<?php

namespace app\admin\controller;

use app\model\system\SystemAdmin;
use app\model\system\SystemMenus;
use app\model\system\SystemRole;
use Exception;
use think\App;
use think\facade\Route;
use think\Model;

/**
 * 登录验证基类
 * Class AuthController
 * @package app\admin\controller
 */
class AuthController extends SystemBasic
{
    protected $aid;
    protected $admin;

    /**
     * 当前管理员权限
     * @var array
     */
    protected $auth = [];

    protected $skipLogController = ['index', 'common'];

    /**
     * 初始化
     * Author: Albert3306
     * Date: 2021/5/7
     * @throws Exception
     */
    public function initialize()
    {
        parent::initialize();
        if (!SystemAdmin::hasActiveAdmin()) $this->redirect('/admin/login');
        try {
            $adminInfo = SystemAdmin::activeAdminInfoOrFail();
        } catch (Exception $e) {
            return $this->failed(SystemAdmin::getErrorInfo($e->getMessage()), '/admin/login');
        }

        // 获取登录用户信息
        $this->aid = $adminInfo['id'];
        $this->admin = $adminInfo;
        $this->getActiveAdminInfo();
        $this->admin->level === 0 || $this->checkAuth();

        // 模板变量赋值
        $this->assign([
            'title'     => '',
            '_admin'    => $this->admin,
        ]);
        event('AdminVisit', [$this->admin, 'system']);
    }

    /**
     * 验证权限
     * Author: Albert3306
     * Date: 2021/4/26
     * @param null $action
     * @param null $controller
     * @param null $module
     * @param array $route
     * @return bool
     * @throws Exception
     */
    protected function checkAuth($action = null, $controller = null, $module = null, array $route = []): bool
    {
        static $allAuth = null;
        if ($allAuth === null) $allAuth = SystemRole::getAllAuth();
        if ($module === null) $module = app('http')->getName();
        if ($controller === null) $controller = $this->request->controller();
        if ($action === null) $action = $this->request->action();
        if (!count($route)) $route = $this->request->route();
        if (in_array(strtolower($controller), $this->skipLogController, true)) return true;
        $nowAuthName = SystemMenus::getAuthName($action, $controller, $module, $route);
        $baseNowAuthName = SystemMenus::getAuthName($action, $controller, $module, []);
        if ((in_array($nowAuthName, $allAuth) && !in_array($nowAuthName, $this->auth)) || (in_array($baseNowAuthName, $allAuth) && !in_array($baseNowAuthName, $this->auth)))
            return $this->failed('没有权限访问！');
        return true;
    }

    /**
     * 获得当前用户最新信息
     * @return array|Model
     * @throws Exception
     */
    protected function getActiveAdminInfo()
    {
        $adminId = $this->aid;
        $adminInfo = SystemAdmin::getValidAdminInfoOrFail($adminId);
        if (!$adminInfo) $this->failed(SystemAdmin::getErrorInfo('请登陆!'));
        $this->admin = $adminInfo;
        SystemAdmin::setLoginInfo($adminInfo);
        return $adminInfo;
    }
}